package com.danger.Activity;

import android.content.Intent;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.danger.Model.bean.RegisterUserInfo;
import com.danger.Presenter.RegisterPresenter;
import com.danger.Presenter.RegisterPresenterImpl;
import com.danger.View.RegisterView;

public class RegisterActivity extends AppCompatActivity implements RegisterView, View.OnClickListener {

    private EditText register_username_editText;
    private EditText register_password_editText;
    private EditText register_check_password_editText;
    private EditText register_qq_editText;
    private EditText register_email_editText;
    private Button register_next_button;
    private Toolbar toolbar;
    private RegisterPresenter registerPresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        init();
    }

    private void init() {
        toolbar = (Toolbar) findViewById(R.id.register_toolbar);
        toolbar.setTitle("注册");
        toolbar.setTitleTextColor(Color.WHITE);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        registerPresenter = new RegisterPresenterImpl(getApplicationContext(), this);
        register_username_editText = (EditText) findViewById(R.id.register_username_editText);
        register_password_editText = (EditText) findViewById(R.id.register_password_editText);
        register_check_password_editText = (EditText) findViewById(R.id.register_check_password_editText);
        register_qq_editText = (EditText) findViewById(R.id.register_qq_editText);
        register_email_editText = (EditText) findViewById(R.id.register_email_editText);
        register_next_button = (Button) findViewById(R.id.register_next_button);
        register_next_button.setOnClickListener(this);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.register_next_button:
                boolean flag = registerPresenter.toNext();
                if (flag) {
                    Intent intent = new Intent(RegisterActivity.this, UploadPhotoActivity.class);
                    intent.putExtra("username", register_username_editText.getText().toString());
                    intent.putExtra("password", register_password_editText.getText().toString());
                    intent.putExtra("qq", register_qq_editText.getText().toString());
                    intent.putExtra("email", register_email_editText.getText().toString());
                    startActivity(intent);
                }
                break;
            default:
                break;
        }
    }

    @Override
    public void showError(String msg) {
        Toast.makeText(this, "错误：" + msg, Toast.LENGTH_SHORT).show();
    }

    @Override
    public RegisterUserInfo getRegisterUserInfo() {
        RegisterUserInfo registerUserInfo = new RegisterUserInfo();
        registerUserInfo.setUsername(register_username_editText.getText().toString());
        registerUserInfo.setPassword(register_password_editText.getText().toString());
        registerUserInfo.setCheck_password(register_check_password_editText.getText().toString());
        registerUserInfo.setQq(register_qq_editText.getText().toString());
        registerUserInfo.setEmail(register_email_editText.getText().toString());
        return registerUserInfo;
    }
}
